<?php

    class Entries_Custom_Fields extends ActiveRecord {
        
        function _setup()
        {
            $this->table = DB_PREFIX.'entries_custom_fields';
        }
        
        
        function validate()
        {
            $this->validates_presence_of('custom_field_id');
            $this->validates_numericality_of('custom_field_id');
        }
        
        function after_validation()
        {
            $content = $this->get('field_value');
            if(empty($content)) return;
            require VENDOR.'/htmlpurifier/HTMLPurifier.auto.php';
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Core', 'Encoding', DEFAULT_CHARSET); //replace with your encoding
            $config->set('HTML','Doctype',HTML_DOCTYPE);
            $purifier = new HTMLPurifier($config);

            $clean_html = $purifier->purify($this->get('field_value'));
            
            $this->set('field_value',$clean_html);
        }
        
        function field_type()
        {
            return $this->db->GetOne('select field_type from custom_fields where id=?',
                                    array($this->get('custom_field_id')));
        }
        
        function field_key()
        {
            return $this->db->GetOne('select field_key from custom_fields where id=?',
                                    array($this->get('custom_field_id')));
        }
        
        function value()
        {
            return $this->get('field_value');
        }
        
        function date($format="%D")
        {
            return strftime($format, strtotime($this->get('field_date')));
        }
        
    }

?>